var MembersForm=function(){
var toolbar;
	var grid;
	var fs,win;

	var buildToolbar = function(){
		toolbar = new Ext.Panel({
			border:false,
			tbar:[{
				text:'新增',
				cls:'x-btn-text-icon',
				icon:RES_ROOT_PATH+'images/add.gif',
				handler:function(){
					win.setTitle('添加新用戶');
					win.show();
					fs.form.reset();
					fs.url = './?ctrl=members&action=create';
				}
			},'-',{
				text:'修改',
				cls:'x-btn-text-icon',
				icon:RES_ROOT_PATH+'images/edit.gif',
				handler:function(){
					var rec = grid.getSelectionModel().getSelected();
					if (rec){
						win.setTitle('修改用戶信息');
						win.show();
						fs.form.findField('member[username]').setValue(rec.data.username);
						fs.form.findField('member[nickname]').setValue(rec.data.nickname);
						fs.form.findField('member[group_id]').setValue(rec.data.group_id);
						fs.form.findField('member[email]').setValue(rec.data.email);
						fs.id = rec.data.id;
						fs.url = './?ctrl=members&action=update';
					}
					else{
						Ext.MessageBox.alert('警告','請選擇一個數據。');
					}
				}
			},'-',{
				text:'刪除',
				cls:'x-btn-text-icon',
				icon:RES_ROOT_PATH+'images/delete.gif',
				handler:function(){
					var rec = grid.getSelectionModel().getSelected();
					if (rec){
						Ext.Msg.confirm('提示', '是否确认删除？', function(btn){
							if (btn == 'yes'){
								Ext.Ajax.request({
									url:'./?ctrl=members&action=delete',
									params:{
										id:rec.data.id
									},
									method:'POST',
									success:function(response){
										grid.store.reload();
									}
								});
							}
						},this);
					}else{
						Ext.MessageBox.alert('警告','請選擇一個數據。');
					}
				}
			}]
		});
	};

	var buildGrid = function(){
		var grouprend=function(value ,metadata ,record ,rowIndex ,colIndex ,store ){
			return groupData[value];
		};
		var regdaterend=function(value ,metadata ,record ,rowIndex ,colIndex ,store ){
			return getLocTime(value);
		};
		var rootrend=function(value ,metadata ,record ,rowIndex ,colIndex ,store ){
			if(value==1){
				return "是";
			}
			return "否";
		};
		grid = new Ext.grid.GridPanel({
			store:new Ext.data.Store({
				proxy:new Ext.data.HttpProxy({url:'./?ctrl=members&action=getAll'}),
				reader:new Ext.data.JsonReader({},[
				'id','username','nickname','group_id','email','regdate','root','qq'
				]),
				autoLoad:true
			}),
			columns:[
			new Ext.grid.RowNumberer({header :'ID',dataIndex:'id'}),
			{header:'用戶名',width:150,sortable:true,dataIndex:'username'},
			{header:'昵稱',width:100,sortable:true,dataIndex:'nickname'},
			{header:'所在組',width:150,sortable:true,dataIndex:'group_id',renderer: grouprend},
			{header:'email',width:150,sortable:true,dataIndex:'email'},
			{header:'qq',width:150,sortable:true,dataIndex:'qq'},
			{header:'注冊日期',width:150,sortable:true,dataIndex:'regdate',renderer: regdaterend},
			{header:'超級管理員',width:150,sortable:true,dataIndex:'root',renderer: rootrend}
			],
			renderTo:'grid',
			border:false
		});
	};

	var buildLayout = function(){
		new Ext.Viewport({
			layout:'border',
			defaults:{border:false},
			items:[{
				region:'north',
				height:50,
				title:'通道管理',
				items:toolbar
			},{
				region:'center',
				layout:'fit',
				items:grid
			}]
		});
	};

	var buildForm = function(){
		var groupcomb=new Ext.form.ComboBox({
			fieldLabel: '所在組',
			transform:'member[group_id]',
			mode:'local',
			lazyRender:true,
			listClass: 'x-combo-list-small',
			typeAhead: true,
			readOnly :true,
			triggerAction: 'all',
			selectOnFocus:true,
			allowBlank:false,
			width:"50%"
		});
		fs = new Ext.form.FormPanel({
			labelWidth:90,
			labelAlign:'right',
			defaultType:'textfield',
			defaults:{width:200},
			bodyStyle:'padding:10;',
			frame:true,
			items:[{
				fieldLabel:'用戶名',
				allowBlank:false,
				name:'member[username]'
			},groupcomb,{
				fieldLabel:'密碼',
				name:'member[password][0]',
				inputType : 'password'
			},{
				fieldLabel:'確認密碼',
				name:'member[password][1]',
				inputType : 'password'
			},{
				fieldLabel:'昵稱',
				allowBlank:false,
				name:'member[nickname]'
			},{
				fieldLabel:'email',
				allowBlank:false,
				name:'member[email]'
			},{
				fieldLabel:'qq',
				allowBlank:false,
				name:'member[qq]'
			}]
		});
	};

	var buildWin = function(){
		win = new Ext.Window({
			width:400,
			height:260,
			layout:'fit',
			closeAction:'hide',
			modal:true,
			items:fs,
			buttons:[{
				text:'保存',
				handler:saveChannel
			},{
				text:'取消',
				handler:function(){win.hide();}
			}]
		});
	};

	var saveChannel = function(){
		fs.form.submit({
			waitMsg:'正在保存。。。',
			url:fs.url,
			params:{id:fs.id},
			method:'POST',
			success:function(){
				win.hide();
				grid.store.reload();
			}
		});
	};
	return{
		init:function(){
			buildToolbar();
			buildGrid();
			buildLayout();
			buildForm();
			buildWin();
		}
	}
}();
Ext.onReady(MembersForm.init,MembersForm);